package com.bz.common;

import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class HttpUtil {

    // 使用HttpClient 进行doGET请求
    public static String doGet(String url) {
        String result = null;
        // DefaultHttpClient httpclient =new DefaultHttpClient();//老版本的方法
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpget = new HttpGet(url);
        try {
            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                result = EntityUtils.toString(entity, "UTF-8");
            }
        } catch (Exception e) {
            LogUtils.error(log, e, "远程调用异常url:" + url);
            ResponseDto<String> re = new ResponseDto<>(Codeenum.REMOTE_ERROR.getCode(), e.getMessage());
            result = JSON.toJSONString(re);
        } finally {
            try {
                if (httpclient != null) {
                    httpclient.close();
                }
            } catch (IOException e) {
                LogUtils.error(log, e, "远程调用异常url:" + url);
            }
        }
        LogUtils.info(log, "远程调用url:" + url + ",res:" + result);
        return result;
    }

    // 使用HttpClient 进行dopOSTT请求,适合发送的数据为json数据格式
    public static String doPOST(String url, String outstr, Map<String, String> headerMap) {

        // return
        // "{\"words_result_num\":14,\"words_result\":[{\"word\":\"重庆医科大学附属儿童医院\",\"word_name\":\"医院名称\"},{\"word\":\"超声诊断报告单\",\"word_name\":\"报告名称\"},{\"word\":\"刘进杰\",\"word_name\":\"姓名\"},{\"word\":\"男\",\"word_name\":\"性别\"},{\"word\":\"20231219\",\"word_name\":\"报告日期\"},{\"word\":\"渝中泌尿外科病房\",\"word_name\":\"科室\"},{\"word\":\"异体肾移植状态\",\"word_name\":\"临床诊断\"},{\"word\":\"移植肾脏:多切面扫查,移植肾脏位于右下腹,周围可探及异常无回声区含絮条状回声,范围约8.2cm×6.9cm×2.6cm,移植肾大小约9.8cm×5.2cm×5.1cm,形态规则,包膜光滑,皮质回声未见明显增强,皮髓质分界清晰,集合系统未见明显分离,肾盂无回声区宽约1.4cm,CDFI于其内可探及丰富的血流信号,PW测值如下:流速(m/s)RI肾主动脉0.2019570.92/042055肾主静脉1.05/0.46056肾动脉吻合口0.20肾静脉吻合口036/016057叶间动脉0.20叶间静脉输尿管未探及明显打张显示。肪脉不充盈,腔内显示不清。\",\"word_name\":\"检查所见\"},{\"word\":\"输尿管未探及明显打张显示。肪脉不充盈,腔内显示不清。肾移植术后:1移植肾肖主省动静脉吻合口血流信号及频增未见明显异2移植肾轻度积水3移植肾周围积液,内管野维渗出物\",\"word_name\":\"检查提示\"},{\"word\":\"\",\"word_name\":\"肉眼可见\"},{\"word\":\"\",\"word_name\":\"建议\"},{\"word\":\"\",\"word_name\":\"检查部位\"},{\"word\":\"\",\"word_name\":\"年龄\"},{\"word\":\"\",\"word_name\":\"检查方法\"}],\"log_id\":1827664362770311182}";

        // DefaultHttpClient httpclient =new DefaultHttpClient();//老版本的方法
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httppost = new HttpPost(url);
        String result = null;
        try {
            if (null != headerMap) {
                for (Entry<String, String> hm : headerMap.entrySet()) {
                    httppost.setHeader(hm.getKey(), hm.getValue());
                }
            }

            if (!StringUtils.isEmpty(outstr)) {
                httppost.setEntity(new StringEntity(outstr, "UTF-8"));
            }

            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                result = EntityUtils.toString(entity, "UTF-8");
            }

        } catch (Exception e) {
            LogUtils.error(log, e, "远程调用异常url:" + url);
            ResponseDto<String> re = new ResponseDto<>(Codeenum.REMOTE_ERROR.getCode(), e.getMessage());
            result = JSON.toJSONString(re);
        } finally {
            try {
                if (httpclient != null) {
                    httpclient.close();
                }
            } catch (IOException e) {
                LogUtils.error(log, e, "远程调用异常url:" + url);
            }
        }
        LogUtils.info(log, "远程调用url:" + url + ",param:" + outstr + ",res:" + result);
        return result;
    }
}
